import { ApiProperty } from '@nestjs/swagger';
import { Type, Transform } from 'class-transformer';
import { IsOptional, IsInt, Min, Max } from 'class-validator';

/**
 * 分页查询DTO
 */
export class PaginationDto {
  @ApiProperty({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1,
    required: false,
  })
  @IsOptional()
  @IsInt({ message: '页码必须是整数' })
  @Min(1, { message: '页码必须大于0' })
  @Transform(({ value }) => parseInt(value))
  page: number = 1;

  @ApiProperty({
    description: '每页数量',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10,
    required: false,
  })
  @IsOptional()
  @IsInt({ message: '每页数量必须是整数' })
  @Min(1, { message: '每页数量必须大于0' })
  @Max(100, { message: '每页数量不能超过100' })
  @Transform(({ value }) => parseInt(value))
  pageSize: number = 10;

  /**
   * 计算跳过的记录数
   */
  get skip(): number {
    return (this.page - 1) * this.pageSize;
  }

  /**
   * 获取限制数量
   */
  get take(): number {
    return this.pageSize;
  }
}